package servlets;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import beans.CarBean;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import service.CarService;

/**
 * Servlet implementation class CarServlet
 */
public class CarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		if ("admin".equals(action)) {
			admin(request, response);
		}
		if ("admin1".equals(action)) {
			admin1(request, response);
		}
		// ��ѯ
		if ("query".equals(action)) {
			query(request, response);
		}
		if ("query1".equals(action)) {
			query1(request, response);
		}
		// �鿴
		if ("view".equals(action)) {
			view(request, response);
		}
		if ("view1".equals(action)) {
			view1(request, response);
		}
		if ("addCar".equals(action)) {
			try {
				addCar(request, response);
			} catch (ServletException | IOException | ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if ("editCar".equals(action)) {
			try {
				editCar(request, response);
			} catch (ServletException | IOException | ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if ("delCar".equals(action)) {
			delCar(request, response);
		}
		if ("toEdit".equals(action)) {
			toEdit(request, response);
		}
	}

	private void view1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int carId = Integer.parseInt(request.getParameter("id"));
		CarBean car = null;
		CarService carService = new CarService();
		car = carService.getRecord(carId);
		request.getSession().setAttribute("car", car);
		String path = "/D-Car_Details_look.jsp";
		response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
		RequestDispatcher requestDistpatcher = request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);
	}

	private void query1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String asset_number = request.getParameter("asset_number");
		String usage_status = request.getParameter("usage_status");
		String car_name = request.getParameter("car_name");
		String license_plate = request.getParameter("license_plate");
		CarService carService = new CarService();
		List<CarBean> cars = carService.browse(asset_number, usage_status, car_name, license_plate);
		response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
		request.getSession().setAttribute("allCars", cars);
		request.getRequestDispatcher("/D-Car_Admin.jsp").forward(request, response);
	}

	private void admin1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CarService carService = new CarService();
		List<CarBean> cars = carService.browse();
		response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
		request.getSession().setAttribute("allCars", cars);
		request.getRequestDispatcher("/D-Car_Admin.jsp").forward(request, response);
	}

	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String carid = request.getParameter("id");// ȡ�ô��ݹ������û����������ڲ�ѯ��ɾ��
		CarBean car = null;
		CarService carService = new CarService();
		car = carService.getRecord(Integer.parseInt(carid));
		request.getSession().setAttribute("car", car);
		String path = "/C-Car_Details_edit.jsp";
		response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
		RequestDispatcher requestDistpatcher = request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);
	}

	private void view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int carId = Integer.parseInt(request.getParameter("id"));
		CarBean car = null;
		CarService carService = new CarService();
		car = carService.getRecord(carId);
		request.getSession().setAttribute("car", car);
		String path = "/C-Car_Details_look.jsp";
		response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
		RequestDispatcher requestDistpatcher = request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);
	}

	private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String asset_number = request.getParameter("asset_number");
		String usage_status = request.getParameter("usage_status");
		String car_name = request.getParameter("car_name");
		String license_plate = request.getParameter("license_plate");
		CarService carService = new CarService();
		List<CarBean> cars = carService.browse(asset_number, usage_status, car_name, license_plate);
		response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
		request.getSession().setAttribute("allCars", cars);
		request.getRequestDispatcher("/C-Car_Admin.jsp").forward(request, response);
	}

	private void editCar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, ParseException {
		// TODO Auto-generated method stub
		CarService carService = new CarService();
		String id = request.getParameter("id");
		int idInt = Integer.parseInt(id);
		// �����û����󣬲����뵽���ݱ�users��
		CarBean car = new CarBean();
		car.setCarId(idInt);
		car.setCarName(request.getParameter("car_name"));
		car.setCarType(request.getParameter("car_type"));
		car.setLicensePlate(request.getParameter("license_plate"));
		car.setDisplacement(Float.parseFloat(request.getParameter("displacement")));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		String dateString = request.getParameter("start_date");
		date = dateFormat.parse(dateString);
		java.sql.Date start_date = new java.sql.Date(date.getTime());
		car.setStartDate(start_date);
		car.setManagementDepartment(request.getParameter("management_department"));
		car.setResponsiblePerson(request.getParameter("responsible_person"));
		car.setRegisterPerson(request.getParameter("register_person"));
		car.setAssetNumber(request.getParameter("asset_number"));
		dateString = request.getParameter("purchase_date");
		date = dateFormat.parse(dateString);
		java.sql.Date purchase_date = new java.sql.Date(date.getTime());
		car.setPurchaseDate(purchase_date);
		car.setValue(Float.parseFloat(request.getParameter("value")));
		car.setFrameNumber(request.getParameter("frame_number"));
		car.setCarUsage(request.getParameter("car_usage"));
		car.setUsageStatus(request.getParameter("usage_status"));
		car.setUsageDepartment(request.getParameter("usage_department"));
		car.setUsagePerson(request.getParameter("usage_person"));
		dateString = request.getParameter("entry_date");
		date = dateFormat.parse(dateString);
		java.sql.Date entry_date = new java.sql.Date(date.getTime());
		car.setEntryDate(entry_date);
		if (carService.editCar(car, car)) {
			List<CarBean> list = carService.browse();
			request.getSession().setAttribute("allCars", list);
			String path = "/C-Car_Admin.jsp";
			response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
			RequestDispatcher requestDistpatcher = request.getRequestDispatcher(path);
			requestDistpatcher.forward(request, response);
		}
	}

	private void delCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CarService carService = new CarService();
		// ��ȡ������ת��Ϊ��Ӧ������
		String id = request.getParameter("id");
		int idInt = Integer.parseInt(id);
		// �����û����󣬲����뵽���ݱ�users��
		CarBean car = new CarBean();
		car.setCarId(idInt);
		int delResult = carService.delCar(car);
		if (delResult == 1) {
			List<CarBean> list = carService.browse();
			request.getSession().setAttribute("allCars", list);
			String path = "/C-Car_Admin.jsp";
			response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
			RequestDispatcher requestDistpatcher = request.getRequestDispatcher(path);
			requestDistpatcher.forward(request, response);
		}
	}

	private void addCar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, ParseException {
		// TODO Auto-generated method stub
		CarService carService = new CarService();
		// �����û����󣬲����뵽���ݱ�car_info��
		CarBean car = new CarBean();
		car.setCarName(request.getParameter("car_name"));
		car.setCarType(request.getParameter("car_type"));
		car.setLicensePlate(request.getParameter("license_plate"));
		car.setDisplacement(Float.parseFloat(request.getParameter("displacement")));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		String dateString = request.getParameter("start_date");
		date = dateFormat.parse(dateString);
		java.sql.Date start_date = new java.sql.Date(date.getTime());
		car.setStartDate(start_date);
		car.setManagementDepartment(request.getParameter("management_department"));
		car.setResponsiblePerson(request.getParameter("responsible_person"));
		car.setRegisterPerson(request.getParameter("register_person"));
		car.setAssetNumber(request.getParameter("asset_number"));
		dateString = request.getParameter("purchase_date");
		date = dateFormat.parse(dateString);
		java.sql.Date purchase_date = new java.sql.Date(date.getTime());
		car.setPurchaseDate(purchase_date);
		car.setValue(Float.parseFloat(request.getParameter("value")));
		car.setFrameNumber(request.getParameter("frame_number"));
		car.setCarUsage(request.getParameter("car_usage"));
		car.setUsageStatus(request.getParameter("usage_status"));
		car.setUsageDepartment(request.getParameter("usage_department"));
		car.setUsagePerson(request.getParameter("usage_person"));
		dateString = request.getParameter("entry_date");
		date = dateFormat.parse(dateString);
		java.sql.Date entry_date = new java.sql.Date(date.getTime());
		car.setEntryDate(entry_date);
		if (carService.addCar(car)) {
			List<CarBean> list = carService.browse();
			request.getSession().setAttribute("allCars", list);
			String path = "/C-Car_Admin.jsp";
			response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
			RequestDispatcher requestDistpatcher = request.getRequestDispatcher(path);
			requestDistpatcher.forward(request, response);
		}
	}

	private void admin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CarService carService = new CarService();
		List<CarBean> cars = carService.browse();
		response.setContentType("text/html;charset=UTF-8");// ��ֹ���벻һ�³����������룬�˾�ǰ������out���
		request.getSession().setAttribute("allCars", cars);
		request.getRequestDispatcher("/C-Car_Admin.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
